const getMysql = require('../mysql')

const homePage = async(req,res)=>{
    let sql = 'select * from tarvel_show where ids<=50'
    getMysql(sql).then(result=>{
        res.send({
            code:200,
            message:'查询成功',
            data:result
        })
    }).catch(err=>{
        res.send({
            code:202,
            message:'查询失败',
        })
    })
}

const tabMid = async(req,res)=>{
    let mid = Number(req.query.mid)
    // console.log(mid);
    // let pno = Number((req.query.pno-1)*6)
    let sql = 'select * from tarvel_show where mid = ?'
    getMysql(sql,mid).then(result=>{
        res.send(result)
    }).catch(err=>{
        res.send(err)
        console.log(err);
    })
}

// 通过id查找到详情页
const detailMid = async(req,res)=>{
    // console.log(req.query.ids);
    let sql = 'select * from tarvel_show where ids = ?'
    getMysql(sql,req.query.ids).then(result=>{
        res.send({
            code:200,
            message:'查询成功！',
            data:result
        })
    }).catch(err=>{
        res.send({
            code:202,
            message:'查询失败'
        })
    })
}


// 演出页搜索
const search = async(req,res)=>{
    // console.log(req.body.title1);
    let title1 = `%${req.body.title1}%`
    let sql = 'select * from tarvel_show where title1 like ?' 
    getMysql(sql,title1).then(result=>{
        res.send({
            code:200,
            message:'查询成功！',
            data:result
        })
    }).catch(err=>{
        console.log(err);
        res.send({
            code:202,
            message:'查询失败',
        })
    })

}

// 演出收藏
const shoeCollect = async (req, res) => {
    let { username,name,ids,title,image,price,address,iphone,timer} = req.body;
    let sql = 'insert into show_collect(title, image, price, username,ids,timer,name,iphone,address) values (?,?,?,?,?,?,?,?,?)'
    getMysql(sql, title, image, price, username,ids,timer,name,iphone,address).then(result => {
        res.send({
            coded: 200,
            message: '插入成功！！！',
            // data:result
        })
    }).catch(err => {
        console.log(err);
        res.send({
            coded: 201,
            message: '插入失败',
        })
    })
}




module.exports = {
    homePage,
    tabMid,
    detailMid,
    search,
    shoeCollect
}